package com.ruoyi.sycs.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.sycs.domain.bo.UseFanganQueryBo;
import com.ruoyi.sycs.domain.vo.BaFanganDetailVo;
import com.ruoyi.sycs.domain.vo.BaFanganYuesheVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.sycs.domain.vo.BsFanganInfoVo;
import com.ruoyi.sycs.domain.bo.BsFanganInfoBo;
import com.ruoyi.sycs.service.IBsFanganInfoService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 小数点方案管理
 *
 * @author zyb
 * @date 2024-10-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sycs/bsFanganInfo")
public class BsFanganInfoController extends BaseController {

    private final IBsFanganInfoService iBsFanganInfoService;

    /**
     * 查询小数点方案管理列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<BsFanganInfoVo> list(BsFanganInfoBo bo, PageQuery pageQuery) {
        if (ObjUtil.isNull(bo.getDeptId())) {
            throw new ServiceException("deptId不能为空");
        }
        if (ObjUtil.isNull(bo.getExcelTypeId())) {
            throw new ServiceException("excelTypeId不能为空");
        }
        return iBsFanganInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小数点方案管理列表
     */
    @SaCheckLogin
    @Log(title = "小数点方案管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BsFanganInfoBo bo, HttpServletResponse response) {
        List<BsFanganInfoVo> list = iBsFanganInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "小数点方案管理", BsFanganInfoVo.class, response);
    }

    /**
     * 获取小数点方案管理详细信息
     *
     */
    @SaCheckLogin
    @GetMapping("/{fanganId}")
    public R<BsFanganInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long fanganId) {
        return R.ok(iBsFanganInfoService.queryById(fanganId));
    }

    /**
     * 查询当前使用的方案
     */
    @SaCheckLogin
    @GetMapping("/factUseFangan")
    public R<BsFanganInfoVo> getFactUseFangan(@Validated(QueryGroup.class) UseFanganQueryBo bo) {
        return R.ok(iBsFanganInfoService.getFactUseFangan(bo));
    }

    /**
     * 新增小数点方案管理
     */
    @SaCheckLogin
    @Log(title = "小数点方案管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BsFanganInfoBo bo) {
        return toAjax(iBsFanganInfoService.insertByBo(bo));
    }

    /**
     * 修改小数点方案管理
     */
    @SaCheckLogin
    @Log(title = "小数点方案管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BsFanganInfoBo bo) {
        return toAjax(iBsFanganInfoService.updateByBo(bo));
    }

    /**
     * 删除小数点方案
     *
     * @param fanganIds 主键串
     */
    @SaCheckLogin
    @Log(title = "小数点方案管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fanganIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] fanganIds) {
        return toAjax(iBsFanganInfoService.deleteWithValidByIds(Arrays.asList(fanganIds), true));
    }

    /**
     * 设为试验室默认方案
     */
    @SaCheckLogin
    @Log(title = "小数点方案管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @GetMapping("/setDefault/{fanganId}")
    public R<Void> setDefault( @NotNull(message = "fanganId不能为空") @PathVariable Long fanganId) {
        return toAjax(iBsFanganInfoService.setDefault(fanganId));
    }

    /**
     *  新增方案时，获取方案详细的预设字段信息（新增方案前调用）
     */
    @SaCheckLogin
    @PostMapping("/getYusheInfo")
    public R<List<BaFanganYuesheVo>> getYusheInfo(@RequestBody BsFanganInfoBo bo) {
        Assert.notNull(bo.getExcelTypeId(), "excelTypeId不能为空");
        return R.ok(iBsFanganInfoService.getYusheInfo(bo));
    }


}
